package patrol.mobile.com.mobilepatrol.activity.office;

import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.fragment.OtherBusinessSearchFragment;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

/**
 * Created by K on 2018/9/10.
 * des: 其他业务
 */
public class OtherBusinessSearchNewActivity extends BaseActivity {

    @BindView(R.id.tab_layout_other_business)
    TabLayout tabLayoutOtherBusiness;
    @BindView(R.id.vp_other_business)
    ViewPager vpOtherBusiness;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_other_business;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(OtherBusinessSearchNewActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("其他业务查询");
    }

    @Override
    public void initData() {
        super.initData();
        TabFragmentAdapter adapter = new TabFragmentAdapter(getSupportFragmentManager(), this);
        vpOtherBusiness.setAdapter(adapter);
        tabLayoutOtherBusiness.setupWithViewPager(vpOtherBusiness);
        switchTab(0);
    }

    private void switchTab(int index) {
        if (vpOtherBusiness != null) {
            vpOtherBusiness.setCurrentItem(index, false);
        }
    }

    /*
     * 此结构为activity+fragment+fragment
     */
    static class TabFragmentAdapter extends FragmentStatePagerAdapter {
        private String[] TITLES;

        private Fragment[] tabs;

        private TabFragmentAdapter(FragmentManager fm, Activity context) {
            super(fm);
            TITLES = context.getResources().getStringArray(R.array.other_business_activity_titles);
            tabs = new Fragment[TITLES.length];
            tabs[0] = OtherBusinessSearchFragment.newInstance("0","");//未完成
            tabs[1] = OtherBusinessSearchFragment.newInstance("1","");//已完成
        }

        @Override
        public Fragment getItem(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }

    @Override
    public void setListener() {

    }
}
